

package org.redrider.shop.domain

class PartsOrderController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ partsOrderInstanceList: PartsOrder.list( params ), partsOrderInstanceTotal: PartsOrder.count() ]
    }

    def show = {
        def partsOrderInstance = PartsOrder.get( params.id )

        if(!partsOrderInstance) {
            flash.message = "PartsOrder not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ partsOrderInstance : partsOrderInstance ] }
    }

    def delete = {
        def partsOrderInstance = PartsOrder.get( params.id )
        if(partsOrderInstance) {
            try {
                partsOrderInstance.delete(flush:true)
                flash.message = "PartsOrder ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "PartsOrder ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "PartsOrder not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def partsOrderInstance = PartsOrder.get( params.id )

        if(!partsOrderInstance) {
            flash.message = "PartsOrder not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ partsOrderInstance : partsOrderInstance ]
        }
    }

    def update = {
        def partsOrderInstance = PartsOrder.get( params.id )
        if(partsOrderInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(partsOrderInstance.version > version) {
                    
                    partsOrderInstance.errors.rejectValue("version", "partsOrder.optimistic.locking.failure", "Another user has updated this PartsOrder while you were editing.")
                    render(view:'edit',model:[partsOrderInstance:partsOrderInstance])
                    return
                }
            }
            partsOrderInstance.properties = params
            if(!partsOrderInstance.hasErrors() && partsOrderInstance.save()) {
                flash.message = "PartsOrder ${params.id} updated"
                redirect(action:show,id:partsOrderInstance.id)
            }
            else {
                render(view:'edit',model:[partsOrderInstance:partsOrderInstance])
            }
        }
        else {
            flash.message = "PartsOrder not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def partsOrderInstance = new PartsOrder()
        partsOrderInstance.properties = params
        return ['partsOrderInstance':partsOrderInstance]
    }

    def save = {
        def partsOrderInstance = new PartsOrder(params)
        if(!partsOrderInstance.hasErrors() && partsOrderInstance.save()) {
            flash.message = "PartsOrder ${partsOrderInstance.id} created"
            redirect(action:show,id:partsOrderInstance.id)
        }
        else {
            render(view:'create',model:[partsOrderInstance:partsOrderInstance])
        }
    }
}
